package dian.qing.li.demo.security.utils;

import dian.qing.li.demo.security.commons.RestException;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.Map;

@UtilityClass
public class Assert {

    public static void isFalse(boolean expression, String message) {
        if (expression) {
            throw new RestException(1, message);
        }
    }

    public static void isTrue(boolean expression, String message) {
        isFalse(!expression, message);
    }

    public static void isNotNull(@Nullable Object object, String message) {
        isFalse(object == null, message);
    }

    public static void isNull(@Nullable Object object, String message) {
        isFalse(object != null, message);
    }

    public static void isNotBlank(@Nullable String text, String message) {
        isFalse(StringUtils.isBlank(text), message);
    }

    public static void isBlank(@Nullable String text, String message) {
        isFalse(StringUtils.isNotBlank(text), message);
    }

    public static void isNotContains(@Nullable String text, String search, String message) {
        isFalse(StringUtils.contains(text, search), message);
    }

    public static void isContains(@Nullable String text, String search, String message) {
        isFalse(!StringUtils.contains(text, search), message);
    }

    public static void startWith(@Nullable String text, String prefix, String message) {
        isFalse(!StringUtils.startsWith(text, prefix), message);
    }

    public static void startNotWith(@Nullable String text, String prefix, String message) {
        isFalse(StringUtils.startsWith(text, prefix), message);
    }

    public static void endWith(@Nullable String text, String prefix, String message) {
        isFalse(!StringUtils.endsWith(text, prefix), message);
    }

    public static void endNotWith(@Nullable String text, String prefix, String message) {
        isFalse(StringUtils.endsWith(text, prefix), message);
    }

    public static void isNotEmpty(@Nullable Object[] array, String message) {
        isFalse(ArrayUtils.isEmpty(array), message);
    }

    public static void isEmpty(@Nullable Object[] array, String message) {
        isFalse(ArrayUtils.isNotEmpty(array), message);
    }

    public static void isNotEmpty(@Nullable Collection<?> collection, String message) {
        isFalse(CollectionUtils.isEmpty(collection), message);
    }

    public static void isEmpty(@Nullable Collection<?> collection, String message) {
        isFalse(CollectionUtils.isNotEmpty(collection), message);
    }

    public static void isNotEmpty(@Nullable Map<?, ?> map, String message) {
        isFalse(MapUtils.isEmpty(map), message);
    }

    public static void isEmpty(@Nullable Map<?, ?> map, String message) {
        isFalse(MapUtils.isNotEmpty(map), message);
    }
}
